package com.sdlh.demo.encrypt;

import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;

@Slf4j
public class EncryptionDemo {
    public static final String path = "D:\\workspace\\projects\\demo\\src\\main\\resources\\encryption\\";
    public static final String  path2 = "D:\\workspace\\projects\\demo\\src\\main\\resources\\encryption\\2024.12.14 13-50-08\\";
    public static final String path3 = "D:\\workspace\\projects\\demo\\src\\main\\2024.12.14 13-50-08\\";

    public static void main(String[] args) {
        batchEncrypt();
//        singleEncrypt();
    }

    private static void batchEncrypt() {
        try(Stream<Path> stream = Files.list(Paths.get(path2))) {
//            Files.walk()
            stream.parallel()
                  .forEach(path1 -> {
                      File file = path1.toFile();
                      String name = file.getName();
                      if (name.endsWith(".m00")) {
                          String raw_file = path2 + name;
                          String de_file = path3 + name;
                          EncryptedUtils.encryptFile(raw_file, de_file);
                      }
                  });
        } catch (Exception e) {
            log.error("文件加密失败", e);
        }
    }

    private static void singleEncrypt() {
        String raw_file = path + "20190506063809.rd";
        String de_file = path + "20190506063809.rd.encrypt";
        try {
            EncryptedUtils.encryptFile(raw_file, de_file);
        } catch (Exception e) {
            log.error("文件加密失败", e);
        }
    }
}
